<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: export_functions.php 11 2013-09-21 11:43:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 11 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:43:11 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

defined( '_VALID_XTC' ) or die( 'Direct Access to this location is not allowed.' );
require_once(DIR_FS_INC.'inc.precision.php');
require_once(DIR_FS_INC.'inc.get_qty.php');


function get_tax_rate_export($class_id, $country_id = -1, $zone_id = -1) {
	global $currency, $db; // calculate currencies

	if ( ($country_id == -1) && ($zone_id == -1) ) {
		$country_id = STORE_COUNTRY;
		$zone_id = STORE_ZONE;
	}

	$tax = $db->db_query("SELECT sum(tax_rate) as tax_rate from " . TABLE_TAX_RATES . " tr left join " . TABLE_ZONES_TO_GEO_ZONES . " za on (tr.tax_zone_id = za.geo_zone_id) left join " . TABLE_GEO_ZONES . " tz on (tz.geo_zone_id = tr.tax_zone_id) WHERE (za.zone_country_id is null or za.zone_country_id = '0' or za.zone_country_id = '" . $country_id . "') and (za.zone_id is null or za.zone_id = '0' or za.zone_id = '" . $zone_id . "') and tr.tax_class_id = '" . $class_id . "' group by tr.tax_priority");

	if ($tax->_numOfRows) {
		$tax_multiplier = 1.0;

		while (!$tax->EOF) {
			$tax_multiplier *= 1.0 + ($tax->fields['tax_rate'] / 100);
			$tax->MoveNext();
		}
		
		return ($tax_multiplier - 1.0) * 100;
	} else 
		return 0;
}

function get_products_price_export($products_id,$price_special,$quantity,$group_id=0,$add_tax=1,$currency,$calculate_currencies=true) {
	global $db;

        $product_price = $db->db_query("SELECT   
											products_price,
											products_discount_allowed,
											products_tax_class_id
										FROM 
											". TABLE_PRODUCTS ."
										WHERE
											products_id = '".$products_id."'");
											
        $price_data=array();
        $price_data=array(
                    'PRODUCTS_PRICE'=>$product_price->fields['products_price'],
                    'PRODUCTS_DISCOUNT_ALLOWED'=>$product_price->fields['products_discount_allowed'],
                    'PRODUCT_TAX_CLASS_ID'=>$product_price->fields['products_tax_class_id']
                    );
					
        $products_tax=get_tax_rate_export($price_data['PRODUCT_TAX_CLASS_ID']);

        if ($add_tax =='0') 
            $products_tax='';
        
        if ($special_price=get_products_special_price($products_id)) {
            $special_price= (add_tax($special_price,$products_tax));
            $price_data['PRODUCTS_PRICE']= (add_tax($price_data['PRODUCTS_PRICE'],$products_tax));
            $price_string=format_special_price_export($special_price,$price_data['PRODUCTS_PRICE'],$price_special,$calculate_currencies=true,$quantity,$products_tax,$add_tax,$currency);
        } else {
            $group_price_data = $db->db_query("	SELECT 
													personal_offer
												FROM 
													personal_offers_by_customers_status_".$group_id."
												WHERE 
													products_id='".$products_id."'");
													
            if($group_price_data->fields['personal_offer']!='' and $group_price_data->fields['personal_offer']!='0.0000') {
                $price_string=$group_price_data->fields['personal_offer'];
                $qty = get_qty($products_id);
                     
				if (!get_qty($products_id)) 
						$qty=$quantity;

				$graduated_price_data = $db->db_query("SELECT 
														max(quantity)
													FROM 
														personal_offers_by_customers_status_".$group_id."
													WHERE 
														products_id='".$products_id."'
													AND 
														quantity<='".$qty."'");

				$graduated_price_data=$db->db_query("	SELECT 
															personal_offer
														FROM 
															personal_offers_by_customers_status_".$group_id."
														WHERE 
															products_id='".$products_id."'
														AND 
															quantity='".$graduated_price_data->fields['max(quantity)']."'");
																
                $price_string=$graduated_price_data->fields['personal_offer'];
                $price_string= (add_tax($price_string,$products_tax));

            } else
               $price_string= (add_tax($price_data['PRODUCTS_PRICE'],$products_tax)); //*$quantity;

            $price_string=$price_string*$quantity;

			$currencies_value = $db->db_query("	SELECT *
												FROM 
													". TABLE_CURRENCIES ." 
												WHERE
													code = '".$currency."'");

			$currencies_data=array();
			$currencies_data=array(
						   'SYMBOL_LEFT'=>$currencies_value->fields['symbol_left'] ,
						   'SYMBOL_RIGHT'=>$currencies_value->fields['symbol_right'] ,
						   'DECIMAL_PLACES'=>$currencies_value->fields['decimal_places'] ,
						   'VALUE'=> $currencies_value->fields['value']);

			if ($calculate_currencies=='true')
				$price_string=$price_string * $currencies_data['VALUE'];
          
			$price_string=precision($price_string,$currencies_data['DECIMAL_PLACES']);

			if ($price_special=='1') {
				$currencies_value = $db->db_query("	SELECT *
													FROM 
														". TABLE_CURRENCIES ." 
													WHERE
														code = '".$currency ."'");

				$price_string=number_format($price_string,$currencies_data['DECIMAL_PLACES'], $currencies_value->fields['decimal_point'], $currencies_value->fields['thousands_point']);

				if ($show_currencies == 1)
					$price_string = $currencies_data['SYMBOL_LEFT']. ' '.$price_string.' '.$currencies_data['SYMBOL_RIGHT'];
            
			}

        }
    return $price_string;
}


function format_special_price_export ($special_price,$price,$price_special,$calculate_currencies,$quantity,$products_tax,$add_tax,$currency){
    global $currency, $db;
	
    $currencies_value = $db->db_query("	SELECT 
											symbol_left,
											symbol_right,
											decimal_places,
											decimal_point,
											thousands_point,
											value
										FROM 
											". TABLE_CURRENCIES ." 
										WHERE
											code = '".$currency ."'");

    $currencies_data=array();
    $currencies_data=array(
                            'SYMBOL_LEFT'=>$currencies_value->fields['symbol_left'] ,
                            'SYMBOL_RIGHT'=>$currencies_value->fields['symbol_right'] ,
                            'DECIMAL_PLACES'=>$currencies_value->fields['decimal_places'],
                            'DEC_POINT'=>$currencies_value->fields['decimal_point'],
                            'THD_POINT'=>$currencies_value->fields['thousands_point'],
                            'VALUE'=> $currencies_value->fields['value'])                            ;
    if ($add_tax =='0') 
        $products_tax='';
    
    $price=$price*$quantity;
    $special_price=$special_price*$quantity;

    if ($calculate_currencies=='true') {
		$special_price=$special_price * $currencies_data['VALUE'];
		$price=$price * $currencies_data['VALUE'];
    }
    // round price
    $special_price=precision($special_price,$currencies_data['DECIMAL_PLACES'] );
    $price=precision($price,$currencies_data['DECIMAL_PLACES'] );
    return $special_price;
}
?>